<?php

namespace app\api\model;

use app\common\constant\RedisConstant;
use app\common\util\Mail;
use think\Model;

class UserNotice extends Model
{

    public static function getNoticeListByUserId($userId, $pageNo, $pageSize)
    {
        return self::where('user_id', $userId)
            ->whereOr('user_id', 0)
            ->page($pageNo, $pageSize)
            ->order('id desc')
            ->select()
            ->toArray();
    }

    public static function writeNotice($userId, $title, $describe, $content)
    {
        return self::insert([
            'user_id' => $userId,
            'title' => $title,
            'describe' => $describe,
            'content' => $content,
            'create_time' => date("Y-m-d H:i:s")
        ]);
    }

    public static function recordUserAction($action, $actionUserInfo, $bookInfo, $recordInfo = [], $oldRecordInfo = [])
    {
        global $redisDB;
        $ownerInfo = UserInfo::getInfoById($bookInfo['owner_id']);
        switch ($action) {
            case 'view':
                $content = '<p>用户<span style="color: #3598db;">' . $actionUserInfo['user_name'] . '</span>查看了您的礼册《' . $bookInfo['book_title'] . '》</p>
<p>查看时间：' . date('Y-m-d H:i:s') . '</p>';
                if (!$redisDB->get_exists(RedisConstant::Notice_Refresh_Time . $actionUserInfo['id'], 'config')) {
                    if ($ownerInfo['user_email'] && $ownerInfo['view_notice']) {
                        Mail::go($ownerInfo['user_email'], '查看礼册通知', $content);
                    }
                    self::writeNotice($bookInfo['owner_id'], '用户查看通知', '用户' . $actionUserInfo['user_name'] . '查看了您的礼册《' . $bookInfo['book_title'] . '》', $content);
                }
                break;
            case 'add':
                $content = '<p>用户<span style="color: #3598db;">' . $actionUserInfo['user_name'] . '</span>在您的礼册《' . $bookInfo['book_title'] . '》添加了一条记录</p>
<p><span style="color: #3598db;">记录名称：' . $recordInfo['record_title'] . '</span></p>
<p><span style="color: #3598db;">记录时间：' . $recordInfo['record_time'] . '</span></p>
<p><span style="color: #3598db;">记录金额：' . $recordInfo['record_value'] . '</span></p>
<p><span style="color: #3598db;">记录备注：' . $recordInfo['record_remark'] . '</span></p>
<p>添加时间：' . date('Y-m-d H:i:s') . '</p>';
                if ($ownerInfo['user_email'] && $ownerInfo['add_notice']) {
                    Mail::go($ownerInfo['user_email'], '添加记录通知', $content);
                }
                $redisDB->set_time(RedisConstant::Notice_Refresh_Time . $actionUserInfo['id'], 'add', 3, 'config');
                self::writeNotice($bookInfo['owner_id'], '添加记录通知', '用户' . $actionUserInfo['user_name'] . '在您的礼册《' . $bookInfo['book_title'] . '》添加了一条记录', $content);
                break;
            case 'edit':
                $content = '<p>用户<span style="color: #3598db;">' . $actionUserInfo['user_name'] . '</span>在您的礼册《' . $bookInfo['book_title'] . '》内修改了一条记录</p>
<p><span style="color: #3598db;">记录名称：' . $oldRecordInfo['record_title'] . ' -> ' . $recordInfo['record_title'] . '</span></p>
<p><span style="color: #3598db;">记录时间：' . $oldRecordInfo['record_time'] . ' -> ' . $recordInfo['record_time'] . '</span></p>
<p><span style="color: #3598db;">记录金额：' . $oldRecordInfo['record_value'] . ' -> ' . $recordInfo['record_value'] . '</span></p>
<p><span style="color: #3598db;">记录备注：' . $oldRecordInfo['record_remark'] . ' -> ' . $recordInfo['record_remark'] . '</span></p>
<p>修改时间：' . date('Y-m-d H:i:s') . '</p>';
                if ($ownerInfo['user_email'] && $ownerInfo['edit_notice']) {
                    Mail::go($ownerInfo['user_email'], '修改记录通知', $content);
                }
                $redisDB->set_time(RedisConstant::Notice_Refresh_Time . $actionUserInfo['id'], 'edit', 3, 'config');
                self::writeNotice($bookInfo['owner_id'], '修改记录通知', '用户' . $actionUserInfo['user_name'] . '在您的礼册《' . $bookInfo['book_title'] . '》内修改了一条记录', $content);
                break;
            case 'delete':
                $content = '<p>用户<span style="color: #3598db;">' . $actionUserInfo['user_name'] . '</span>在您的礼册《' . $bookInfo['book_title'] . '》删除了一条记录</p>
<p><span style="color: #3598db;">记录名称：' . $oldRecordInfo['record_title'] . '</span></p>
<p><span style="color: #3598db;">记录时间：' . $oldRecordInfo['record_time'] . '</span></p>
<p><span style="color: #3598db;">记录金额：' . $oldRecordInfo['record_value'] . '</span></p>
<p><span style="color: #3598db;">记录备注：' . $oldRecordInfo['record_remark'] . '</span></p>
<p>添加时间：' . date('Y-m-d H:i:s') . '</p>';
                if ($ownerInfo['user_email'] && $ownerInfo['delete_notice']) {
                    Mail::go($ownerInfo['user_email'], '删除记录通知', $content);
                }
                $redisDB->set_time(RedisConstant::Notice_Refresh_Time . $actionUserInfo['id'], 'delete', 3, 'config');
                self::writeNotice($bookInfo['owner_id'], '删除记录通知', '用户' . $actionUserInfo['user_name'] . '在您的礼册《' . $bookInfo['book_title'] . '》删除了一条记录', $content);
                break;
            default:
                break;
        }
    }
}